package com.sdkx.app.ViewModel;

import com.sdkx.app.model.AirWay;
import com.sdkx.app.model.Mark;
import com.sdkx.app.model.TrackLine;
import com.sdkx.app.model.ViewObject;
import com.sdkx.app.model.WayPoint;

import java.util.ArrayList;
import java.util.List;

/**
 * 搜索辅助类
 */
public class SearchHelper {
    /**
     * 搜索所有类别：航线、标记、航点、航迹
     * @param name
     * @return
     */
    public static List<ViewObject> searchInAll(String name) {
        List<ViewObject> objects = new ArrayList<>();
        List<AirWay> airWays = AirWayManager.getItems(name);
        objects.addAll(airWays);
        List<Mark> marks = MarkManager.getItems(name);
        objects.addAll(marks);
        List<WayPoint> wayPoints = WayPointManager.getItems(name);
        objects.addAll(wayPoints);
        List<TrackLine> trackLines = TrackLineManager.getItems(name);
        objects.addAll(trackLines);
        return objects;
    }

    static double EARTH_RADIUS = 6378137;//m 地球半径 平均值，千米
    private static double haverSin(double theta){
        double v = Math.sin(theta/2);
        return v*v;
    }

    /**
     * 给定的经度1，纬度1；经度2，纬度2. 计算2个经纬度之间的距离。
     *
     * @param lat1
     * @param lon1
     * @param lat2
     * @param lon2
     * @return 距离（米）
     */
    public static double distanceOfLatLong(double lon1, double lat1, double lon2, double lat2) {
        //角度转弧度
        lat1 = Math.toRadians(lat1);
        lon1 = Math.toRadians(lon1);
        lat2 = Math.toRadians(lat2);
        lon2 = Math.toRadians(lon2);

        if (lat1 < 0)
            lat1 = Math.PI / 2 + Math.abs(lat1);// south
        if (lat1 > 0)
            lat1 = Math.PI / 2 - Math.abs(lat1);// north
        if (lon1 < 0)
            lon1 = Math.PI * 2 - Math.abs(lon1);// west
        if (lat2 < 0)
            lat2 = Math.PI / 2 + Math.abs(lat2);// south
        if (lat2 > 0)
            lat2 = Math.PI / 2 - Math.abs(lat2);// north
        if (lon2 < 0)
            lon2 = Math.PI * 2 - Math.abs(lon2);// west

        double x1 = EARTH_RADIUS * Math.cos(lon1) * Math.sin(lat1);
        double y1 = EARTH_RADIUS * Math.sin(lon1) * Math.sin(lat1);
        double z1 = EARTH_RADIUS * Math.cos(lat1);

        double x2 = EARTH_RADIUS * Math.cos(lon2) * Math.sin(lat2);
        double y2 = EARTH_RADIUS * Math.sin(lon2) * Math.sin(lat2);
        double z2 = EARTH_RADIUS * Math.cos(lat2);

        double d = Math.sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2) + (z1 - z2) * (z1 - z2));
        //余弦定理求夹角
        double theta = Math.acos((EARTH_RADIUS * EARTH_RADIUS + EARTH_RADIUS * EARTH_RADIUS - d * d) / (2 * EARTH_RADIUS * EARTH_RADIUS));
        double dist = theta * EARTH_RADIUS;
        return dist;
    }
}
